package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 搜索管理
 */
@RestController
@RequestMapping("/api/list")
public class ListApiController {

    //引入  spring-boot-starter-data-elasticsearch   application-dev.yml
    //@Autowired
    //private ElasticsearchTemplate elasticsearchTemplate;//low
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;//high
    @Autowired
    private ListService listService;

    //进行索引库的 mapping 映射
    @GetMapping("/inner/createIndex")
    public Result createIndex(){
        elasticsearchRestTemplate.createIndex(Goods.class);//创建索引库
        elasticsearchRestTemplate.putMapping(Goods.class);//创建库中的属性
        return Result.ok();
    }// nested   Object
    //添加索引  上架索引
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable(name = "skuId") Long skuId){
        listService.upperGoods(skuId);
        return Result.ok();
    }
    //删除索引  下架索引
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable(name = "skuId") Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok();
    }

    //更新ES的热度
    @GetMapping("/inner/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable(name = "skuId") Long skuId) {

        listService.incrHotScore(skuId);
        return Result.ok();
    }


    //第三步：搜索索引库  入参  返回值
    //  get 请求 带入参  url?带参数      ?json串 可以 不能太长 限制 火狐 7900+ 字符
    // post   带入参 请求体中
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }


}
